package com.open.cloud.rbac.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;

public class FileUtil
{
    private static Logger log = LoggerFactory.getLogger(FileUtil.class);
    
    /**
     * 生成本地文件,当前目录只保留1个文件
     * 
     * @param folderPath 要生成文件的存放目录路径
     * @param baseStr 要生成的文件内容
     * @param fileName 要生成的文件名称
     */
    public static void createFileSingle(String folderPath, String baseStr, String fileName)
        throws Exception
    {
        // 清空文件夹
        cleanFolder(folderPath);
        
        // 生成本地文件
        createFile(folderPath, baseStr, fileName);
    }
    
    /**
     * 生成本地文件
     * 
     * @param folderPath 要生成文件的存放目录路径
     * @param baseStr 要生成的文件内容
     * @param fileName 要生成的文件名称
     */
    public static String createFile(String folderPath, String baseStr, String fileName)
        throws Exception
    {
        // 生成的文件绝对路径，包括路径和文件名称
        String filePath = "";
        
        BufferedWriter writer = null;
        try
        {
            File f1 = new File(folderPath);
            
            // 如果没有的话就创建目录
            if (!f1.exists())
            {
                f1.setWritable(true);
                f1.mkdirs();
                f1.setReadable(true);
            }
            
            filePath = folderPath + "/" + fileName;
            
            OutputStream out = new FileOutputStream(filePath);
            writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
            
            writer.write(baseStr);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            throw e;
        }
        finally
        {
            try
            {
                if (null != writer)
                {
                    writer.flush();
                    writer.close();
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        
        return filePath;
    }
    
    /**
     * 读入给定路径文件的数据
     * 
     * @param path：给定的路径
     * @return：返回字符串类型的数据
     */
    public static String readFile(String path)
    {
        FileInputStream fis = null;
        StringBuilder strb = new StringBuilder();
        try
        {
            File file = new File(path);
            fis = new FileInputStream(file);
            byte[] b = new byte[fis.available()];
            while (fis.read(b) != -1)
            {
                strb.append(new String(b));
            }
        }
        catch (Exception et)
        {
            et.printStackTrace();
        }
        finally
        {
            try
            {
                if (null != fis)
                {
                    fis.close();
                }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
        
        return strb.toString();
    }
    
    /**
     * 
     * @param path
     */
    private static void grantAuth(String filePath)
    {
        try
        {
            File f1 = new File(filePath);
            
            // 如果没有的话就创建目录
            if (!f1.exists())
            {
                f1.createNewFile();
                f1.setWritable(true);
                f1.setReadable(true);
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
            log.error("FileUtil.grantAuth执行出错，错误信息：" + e.toString(), e);
        }
    }
    
    /**
     * 清空目录
     * 
     * @param path
     * @return
     */
    private static boolean cleanFolder(String path)
    {
        boolean flag = false;
        
        try
        {
            File file = new File(path);
            // 判断是否待删除目录是否存在
            if (!file.exists())
            {
                return true;
            }
            
            // 取得当前目录下所有文件和文件夹
            String[] content = file.list();
            
            for (String name : content)
            {
                File temp = new File(path, name);
                if (temp.exists())
                {
                    temp.delete();
                }
            }
            
            flag = true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        return flag;
    }
    
    public static void main(String[] args)
    {
        String fileTxt = FileUtil.readFile("E:\\tmp_test\\sftp" + "\\" +  "SXYD202005090001.txt");
        System.out.println("fileTxt=" + fileTxt);
        String[] fileTxtArr = fileTxt.split("\r\n");
        System.out.println("fileTxtArr=" + fileTxtArr.length);
    }
}
